<?php

	fta_registerModules(
		array(
		"FTANews/FTANews"
		)
	);

	class FTAEvents extends FTANews {
		
		
		function FTAEvents(){
			parent::__construct();
			
			/* SHOULD ALL BE DONE IN PARENT
			global $db;	
			$this->cmsDocument = new CmsDocument($db);
			
			global $FTADateFormat;
			$this->dateFormat = (0 < strlen($FTADateFormat)) ? $FTADateFormat : "d F y";*/
						
		}
		
		function getNews($pageSize = null, $pageNumber = null, $sort = null, $direction = null){
			
			$this->cmsDocument->flush();
			$this->cmsDocument->setActiveDocuments();
			$this->cmsDocument->addDocumentType('docEvent');
			if(!is_null($sort)){
				if(!is_null($direction)){
					$this->cmsDocument->addSort($sort, $direction);
				} else {
					$this->cmsDocument->addSort($sort);
				}
			} else {
				$this->cmsDocument->addSort("documentyear","DESC");
			}
			
			$this->cmsDocument->setPageSize($pageSize);
			$this->cmsDocument->setPageNumber($pageNumber);
			
			$this->cmsDocument->resetIterator();
			
			$allDocuments = $this->cmsDocument->getAllDocuments();
			
			if (!$allDocuments)
				$allDocuments = array();
				
			return $allDocuments;			
		}
				
		function openNews($newsIdentifier){

			$this->cmsDocument->flush();
			$this->cmsDocument->setActiveDocuments();
			$this->cmsDocument->addDocumentType('docEvent');
			
			if (is_numeric( $newsIdentifier ) &&  0 < ($newsIdentifier * 1)){
				$this->cmsDocument->addConstraint('documentcode',$newsIdentifier);	
			} else {
				$this->cmsDocument->addConstraint('<<md.heading>>',$newsIdentifier);
			}
			
			$this->cmsDocument->setPageSize(1);
			
			$this->cmsDocument->resetIterator();
			
			$allDocuments = $this->cmsDocument->getAllDocuments();

			//echo $this->cmsDocument->documentIteratorQuery();
			
			if($this->cmsDocument->totalDocuments() == 1) {
				//$this->news = $this->cmsDocument->nextDocument();
				$this->news = $allDocuments[0];
				//print_r($this->news);
				return true;
			} else {
				$this->news = array();		
				return false;
			}
						
		}
		
		function renderEventsStartDate(){
			if(is_null($this->news))
				return;				
			
			echo fta_widgetH5Tag(
				date($this->dateFormat,strtotime($this->news->startdate)), ""
			);				
		}
		
		function renderEventsLocation(){
			if(is_null($this->news))
				return;				
			
			echo $this->news->author;				
		}
		
		function renderEventsEndDate(){
			if(is_null($this->news))
				return;				
			
			echo fta_widgetH5Tag(
				date($this->dateFormat,strtotime($this->news->enddate))
			);				
		}
		
		function renderNewsImage($elementId = 11752, $filetypecode = 2 ){
			if(is_null($this->news))
				return;	
			if($this->news->authorbio == '')
				return;	
			echo "<img class=\"imgL\" src=\"/html/blob.php?documentCode=" . $this->news->documentcode . "&filetypecode=" . $filetypecode . "&elementId=" . $elementId . "\" alt=\"" . $this->news->heading . "\" />";			
		}
		
	}
	
	
//

	// news content
	// news list/filter
	
	function fta_getEventsSubNavigation(){		
		ob_start();
		$newsObject = fta_getSharedEventsObject();
		$newsObject->renderNewsSubNavigation();
		return ob_get_clean();				
	}
	
	
	function fta_getEventsList($pageSize = null, $pageNumber = null, $sort = null, $direction = null){		
		ob_start();
		$newsObject = fta_getSharedEventsObject();
		$newsObject->renderNewsList($pageSize, $pageNumber, $sort, $direction);
		return ob_get_clean();				
	}
	
	
	function fta_getLatestEventsList(){		
		ob_start();
		$newsObject = fta_getSharedEventsObject();
		$newsObject->renderLatestNewsList();
		return ob_get_clean();				
	}
	
	
	function fta_getEventsDate($newsIdentifier){
		ob_start();
		fta_eventsOpenEventsAndInvoke($newsIdentifier, 'renderNewsDate');		
		return ob_get_clean();			
	}
	
	function fta_getEventsStartDate($newsIdentifier){
		ob_start();
		fta_eventsOpenEventsAndInvoke($newsIdentifier, 'renderEventsStartDate');		
		return ob_get_clean();			
	}
	
	function fta_getEventsEndDate($newsIdentifier){
		ob_start();
		fta_eventsOpenEventsAndInvoke($newsIdentifier, 'renderEventsEndDate');		
		return ob_get_clean();			
	}
	
	function fta_getEventsLocation($newsIdentifier){
		ob_start();
		fta_eventsOpenEventsAndInvoke($newsIdentifier, 'renderEventsLocation');		
		return ob_get_clean();			
	}
	
	function fta_getEventsHeading($newsIdentifier){
		ob_start();
		fta_eventsOpenEventsAndInvoke($newsIdentifier, 'renderNewsHeading');		
		return ob_get_clean();				
	}
	
///

	function fta_getEventsTitle($newsIdentifier){
		ob_start();
		fta_eventsOpenEventsAndInvoke($newsIdentifier, 'renderNewsTitle');		
		return ob_get_clean();			
	}
	
	function fta_getEventsKeywords($newsIdentifier){
		ob_start();
		fta_eventsOpenEventsAndInvoke($newsIdentifier, 'renderNewsKeywords');		
		return ob_get_clean();			
	}
	
	function fta_getEventsDescription($newsIdentifier){
		ob_start();
		fta_eventsOpenEventsAndInvoke($newsIdentifier, 'renderNewsDescription');		
		return ob_get_clean();			
	}
	
	function fta_getEventsMetaData($newsIdentifier){
		$metaData="";
		$metaData .= fta_getEventsTitle($newsIdentifier);
		$metaData .= fta_getEventsKeywords($newsIdentifier);
		$metaData .= fta_getEventsDescription($newsIdentifier);
		return $metaData;	
	}
	
	
////

	function fta_getEventsContent($newsIdentifier){
		ob_start();
		fta_eventsOpenEventsAndInvoke($newsIdentifier, 'renderNewsContent');		
		return ob_get_clean();			
	}
	
	
	function fta_getEventsImage($newsIdentifier, $elementId = 11752, $filetypecode = 2){
		ob_start();
		$newsObject = fta_getSharedEventsObject();
		$newsObject->openNews( $newsIdentifier );
		$newsObject->renderNewsImage($elementId, $filetypecode);
		return ob_get_clean();
	}
	
	
	function fta_getEvents($newsIdentifier){
		$news = "";
		$news .= fta_getEventsHeading($newsIdentifier);
		$news .= fta_getEventsDate($newsIdentifier);
		$news .= fta_getEventsContent($newsIdentifier);
		return $news;
	}
	
	
	function fta_getEventsTicker(){
		ob_start();
		$newsObject = fta_getSharedEventsObject();
		$newsObject->renderEventsTicker();
		return ob_get_clean();		
	}
	
	//this will rely on voodoo..
	function fta_getEventsRSSLink(){}
	
//////	
	
	function fta_eventsOpenEventsAndInvoke($newsIdentifier,$method){
		$newsObject = fta_getSharedEventsObject();
		if ( $newsObject->openNews( $newsIdentifier ) ) {
			$newsObject->$method();
		}
	}
		
	function fta_getEventsIdentifierFromRequestOrDefault($defaultNewsIdentifier=null){
		$newsIdentifier = extractStringWithKeyFromArray('REQUEST_URI',$_SERVER,null);
		
		// we need to subtract the page scope from the passed in url
		global $fta_;
		$pageScope = $fta_->getPageScope();

		if( preg_match("#^".$pageScope."(.*)/$#",$newsIdentifier,$matches) ){
			$newsIdentifier = urlFragmentCleanRestore($matches[1]);
			
			$nis = split("/",$newsIdentifier);
			if (4 == count($nis)){
				$newsIdentifier = $nis[3];
			} else {
				$newsIdentifier = $defaultNewsIdentifier;
			}

		} else {
			$newsIdentifier = $defaultNewsIdentifier;
		}

		$newsObject = fta_getSharedEventsObject();
				
		if ($newsIdentifier == $defaultNewsIdentifier && is_null($defaultNewsIdentifier)){
			$articles = $newsObject->getNews();
			if(0 <count($articles)){
				$newsIdentifier = $articles[0]->documentcode;
			}
		}
		

		if (!is_null($newsIdentifier) && $newsObject->openNews( $newsIdentifier )){
			return $newsIdentifier;
		}
		
	}
	
	/**
	 * Gets the fta obejct.. these are singletons really..
	 *
	 * @return FTAWebPage
	 */
	function fta_getSharedEventsObject(){

		global $fta_;

		global $FTAEventsClass;
		
		if(!is_null($FTAEventsClass) && "FTAEvents" != $FTAEventsClass){
		} else {
			$FTAEventsClass = "FTAEvents";
		}
			
		if (!array_key_exists('ftaEvents',$fta_)){
			$fta_->ftaEvents = new  $FTAEventsClass();
		}
		return $fta_->ftaEvents;
	}